package com.sun.BmobDemo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;


import com.sun.Utils.toastUtil;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.sms.BmobSMS;
import cn.bmob.sms.exception.BmobException;
import cn.bmob.sms.listener.RequestSMSCodeListener;
import cn.bmob.sms.listener.VerifySMSCodeListener;
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

/*
*本类基于BMOB的各种方法，组合在一个类中
* Bmob_Push_V0.6beta_20150408.jar消息推送jar包
* BmobSDK_V3.4.5_1111  bmob SDK jar包
* BmobSMS_V1.0.1_20150710  短信功能jar包
*
* 需要把JAe包复制到libs中，并设置为依赖
*
**/
public class Bmob0 {

    private Boolean flag;//短信验证成功标志
    private List<UserAccount> useraccount;
    BmobQuery<UserAccount> query = new BmobQuery<UserAccount>();
    public List<BmobObject> queryBeans = new ArrayList<BmobObject>();
    private String currentClassName,toClassName;


    Context context;
    public Bmob0(Context context, String AppID) {
        this.context = context;
        useraccount = new ArrayList<UserAccount>();
        initBmob(AppID);
    }

    //初始化
    private  void initBmob(String AppID){
        //bmob SDK初始化
        cn.bmob.v3.Bmob.initialize(context,AppID);
        //短信初始化
        BmobSMS.initialize(context, AppID);
    }


    //----------------------------短信验证码功能开始--------------------------------------//
    //发送短信验证码  需要传入手机号和短信模板名字
    public void sendSMSCheckedNum(String phoneNum,String s){
        BmobSMS.requestSMSCode(context, phoneNum, s, new RequestSMSCodeListener() {
            @Override
            public void done(Integer integer, BmobException e) {
                if (e == null) {
                    Toast.makeText(context, "验证码发送成功！", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    //短信验证码验证，返回Boolean类型
    public  Boolean checkSmsNum(String phoneNum,String checkNum){
        flag = false;
        BmobSMS.verifySmsCode(context, phoneNum, checkNum, new VerifySMSCodeListener() {
            @Override
            public void done(BmobException e) {
                //验证码验证成功
                if (e == null) {
                    Toast.makeText(context, "正确！", Toast.LENGTH_SHORT).show();
                    flag = true;
                } else {
                    Toast.makeText(context, "验证码不正确！", Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        });
        return flag;
    }

    //----------------------------短信验证码功能结束--------------------------------------//
    //----------------------------网络数据操作功能开始--------------------------------------//

    /**
     * 添加数据
     * 注册（进行重名检测，不重名就添加，重名就返回用户已存在）
     * @param account
     * @param
     * @param
     *
     *
     */
    public  void  regUserAccount(UserAccount account,String curClass,String toClass) {
        //查询是否重名
        useraccount.clear();
        currentClassName = curClass;
       toClassName = toClass;
        useraccount.add(account);
        BmobQuery<UserAccount> query = new BmobQuery<UserAccount>();
        query.addWhereEqualTo("name", account.getName());
        query.findObjects(context, new FindListener<UserAccount>() {
            @Override
            public void onSuccess(List<UserAccount> list) {
                if (list.size() == 0) {
                    addUserAccount();
                } else {
                    toastUtil.toast(context, "用户名已存在！");
                    return;
                }
            }

            @Override
            public void onError(int i, String s) {
                toastUtil.toast(context, "注册失败：" + s.toString());
            }
        });
    }
    //不重名，添加到数据库,并跳转到登录界面
    protected void addUserAccount(){
        BmobQuery<UserAccount> query = new BmobQuery<UserAccount>();
        useraccount.get(0).save(context, new SaveListener() {
            @Override
            public void onSuccess() {
                    toastUtil.toast(context, "注册成功！");
                Log.i("1","bmob");
            }

            @Override
            public void onFailure(int i, String s) {
                toastUtil.toast(context, "注册失败！原因：" + s.toString());
            }
        });
    }


    //删除一条数据
    /*
    public  void  del(String name){
        List<UserAccount> list = queryOneBmobData(name,"name");
        UserAccount user = new UserAccount();
        showToast("123", list);
        if(list != null){

            if(list.size() > 0){
                user.setObjectId(list.get(0).getObjectId());
            }else {
                showToast("数据不存在,无需删除！",null);
            }
        }

        user.delete(context, new DeleteListener() {
            @Override
            public void onSuccess() {
                showToast("删除成功！", null);
            }

            @Override
            public void onFailure(int i, String s) {
//                showToast("删除失败！",s);
            }
        });

    }*/

    /**
     * 数据更新
     * @param
     * @param userAccount 对象
     */

    public void updata(UserAccount userAccount){
        userAccount.update(context, userAccount.getObjectId(), new UpdateListener() {
            @Override
            public void onSuccess() {
//                showToast("更新成功！", null);
            }

            @Override
            public void onFailure(int i, String s) {
//                showToast("更新失败！", s);
            }
        });
    }


    /**
     197.
     * 将子类集合转换为基类BmobObject集合
     198.
     * @param userBeanList
    199.
     * @return
    200.
     */

    private List<BmobObject> convertUserToObject(List<UserAccount> userBeanList){
        queryBeans.clear();
        for(UserAccount userBean: userBeanList){
            queryBeans.add(userBean);
        }
        return queryBeans;
    }


    /**
     * 查询一条数据
     * @param s 查询字符串
     * @param key 取值name phoneNum pwdQusstion
     * @无返回，执行之后需要调用List<UserAccount>
     */
    /*
    public  void queryOneBmobData(String s,String key){
        //account.clear();
//        BmobQuery<UserAccount> oneQuery = new BmobQuery<UserAccount>();
        query.addWhereEqualTo(key, s);
        query.findObjects(context,new FindListener<UserAccount>() {
            @Override
            public void onSuccess(List<UserAccount> list) {
                account = list;
                Toast.makeText(context, "内部"+account.toString(), Toast.LENGTH_SHORT).show();
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("query");
                String str = "";
                for(UserAccount user:account){
                    str+=user.getAname()+""+user.getPhoneNum()+"\n";
                }
                builder.setMessage(str);
                builder.create().show();
            }

            @Override
            public void onError(int i, String s) {
                Toast.makeText(context, "查询失败！"+s.toString(), Toast.LENGTH_SHORT).show();
            }
        });
    }*/


    /**
     * 查询所有数据
     * 无返回，执行之后需要调用List<UserAccount>
     */
   public void queryAll(){
       //account.clear();
//       BmobQuery<UserAccount> query = new BmobQuery<UserAccount>();
       Log.i("1","1");
       query.findObjects(context, new FindListener<UserAccount>() {
           @Override
           public void onSuccess(List<UserAccount> list) {
               convertUserToObject(list);
               //Toast.makeText(context, account.toString(), Toast.LENGTH_SHORT).show();
           }

           @Override
           public void onError(int i, String s) {
               Toast.makeText(context, "查询失败！" + s.toString(), Toast.LENGTH_SHORT).show();
           }
        });
    }

    //----------------------------网络数据操作功能结束--------------------------------------//
    //----------------------------实时消息推送功能开始--------------------------------------//
    //----------------------------实时消息推送功能结束--------------------------------------//
    //----------------------------App推送功能开始--------------------------------------//
    //----------------------------App推送功能结束--------------------------------------//




}
